﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace medder.Registrator
{
    public partial class ViewOrdersDoctor : Form
    {
        MedBaseEntities med = new MedBaseEntities();
        public static ObjectSet<Profile> profiles;
        public static ObjectSet<Doctor> doctors;
        public static ObjectSet<TimeTable> timetables;
        public static ObjectSet<Patient> patients;
        public static ObjectSet<User> users;

        List<Doctor> usedDoctors = new List<Doctor>();
        public ViewOrdersDoctor()
        {
            InitializeComponent();
            profiles = med.Profiles;
            doctors = med.Doctors;
            timetables = med.TimeTables;
            patients = med.Patients;
            users = med.Users;
        }

        private void ViewOrdersDoctor_Load(object sender, EventArgs e)
        {

            bookView.Columns.Add("Number", "№");
            bookView.Columns["Number"].ReadOnly = true;
            bookView.Columns["Number"].Width = 30;

            bookView.Columns.Add("Patient", "Пациент");
            bookView.Columns["Patient"].ReadOnly = true;
            bookView.Columns["Patient"].Width = 235;

            bookView.Columns.Add("Old", "Возраст");
            bookView.Columns["Old"].ReadOnly = true;
            bookView.Columns["Old"].Width = 60;

            bookView.Columns.Add("Address", "Адрес");
            bookView.Columns["Address"].ReadOnly = true;
            bookView.Columns["Address"].Width = 150;


            foreach (var x in profiles)
            {
                comboBox1.Items.Add(x.ProfName);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

                if (!comboBox1.Text.Equals(String.Empty))
                {
                    List<TimeTable> timeTable = new List<TimeTable>();
                    string profile = comboBox1.SelectedItem.ToString();
                    int profId = profiles.Where(x => x.ProfName.Equals(profile)).FirstOrDefault().ProfId;
                    
                    foreach (var table in timetables)
                    {
                        Patient patient = patients.Where(x => x.Passport.Equals(table.PatientID)).FirstOrDefault();
                        Doctor doctor = doctors.Where(x => x.Passport.Equals(table.DoctorID)).FirstOrDefault();
                        int old = GetOld(patient);
                        int currentOld = Convert.ToInt32(numericUpDown1.Value);
                        DateTime start = dateTimePicker1.Value;
                        DateTime end = dateTimePicker1.Value;
                        bool inPeriod = InPeriod(start, end,table.Date.Value);
                        if ((doctor.ProfId==profId) && (old>currentOld) && (inPeriod))
                        {
                            timeTable.Add(table);
                        }
                        

                    }
                    FillTable(timeTable);
                }
            
        }

        private int GetOld (Patient patient)
        {
            return DateTime.Now.Year - patient.Birthday.Value.Year;
        }

        private bool InPeriod(DateTime start, DateTime end, DateTime orderDate)
        {
            start = start.Date;
            end = end.Date;
            if ((orderDate>=start) && (orderDate<=end))
                return true;
            return false;
        }

        private void FillTable(List<TimeTable> timeTable)
        {
            bookView.Rows.Clear();
            int i = 0;
            foreach (TimeTable table in timeTable)
            {
                User patUser = users.Where(x => x.Passport.Equals(table.PatientID)).FirstOrDefault();
                Patient patient = patients.Where(x => x.Passport.Equals(table.PatientID)).FirstOrDefault();
                        
                bookView.Rows.Add();

                bookView.Rows[i].Cells[0].Value = i.ToString();
                bookView.Rows[i].Cells[1].Value = string.Format("{0} {1} {2}", patUser.Surname, patUser.Name, patUser.Patronymic);
                bookView.Rows[i].Cells[2].Value = GetOld(patient).ToString();
                bookView.Rows[i].Cells[3].Value = patient.Address.ToString();
                i++;
            }

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            comboBox1_SelectedIndexChanged(sender, e);
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            comboBox1_SelectedIndexChanged(sender, e);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            comboBox1_SelectedIndexChanged(sender, e);
        }
    }
}
